/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.devoxy.roboeye.R;

import java.io.File;
import java.util.List;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 21.07.11
 * Time: 14:11
 */
public class FileAdapter extends ArrayAdapter<File> {

    private static final String PARENT_DIR = "..";

    private LayoutInflater inflater;
    private int resourceId;

    public FileAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        init(textViewResourceId);
    }

    public FileAdapter(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public FileAdapter(Context context, int textViewResourceId, File[] objects) {
        super(context, textViewResourceId, objects);
        init(textViewResourceId);
    }

    public FileAdapter(Context context, int resource, int textViewResourceId, File[] objects) {
        super(context, resource, textViewResourceId, objects);
        init(resource);
    }

    public FileAdapter(Context context, int textViewResourceId, List<File> objects) {
        super(context, textViewResourceId, objects);
        init(textViewResourceId);
    }

    public FileAdapter(Context context, int resource, int textViewResourceId, List<File> objects) {
        super(context, resource, textViewResourceId, objects);
        init(resource);
    }

    private void init(int resource) {
        inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        resourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = inflater.inflate(resourceId, parent, false);
        } else {
            view = convertView;
        }
        File item = getItem(position);

        TextView tv = (TextView) view.findViewById(R.id.item);
        if (position == 0) {
            tv.setText(PARENT_DIR);
        } else {
            tv.setText(item.getName());
        }

        ImageView icon = (ImageView) view.findViewById(R.id.icon);
        // TODO icons
        if (position == 0) { // level up item
            icon.setBackgroundResource(R.drawable.ic_menu_delete);
        } else if (item.isDirectory()) {
            icon.setBackgroundResource(R.drawable.icon);
        } else {
            icon.setBackgroundResource(R.drawable.home_actionbar);
        }
        return view;
    }
}
